﻿using Microsoft.AspNetCore.Mvc;
using NewRetail.Application.Components.Warehouse;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("promote-sales-fullcut")]
    public class PromoteSalesFullCutController : BaseController<PromoteSalesFullCut>
    {
        private readonly IPromotionSalesFullCutService PromotionSalesFullCutService;
        public PromoteSalesFullCutController(IPromotionSalesFullCutService PromotionSalesFullCutService)
        {
            this.PromotionSalesFullCutService = PromotionSalesFullCutService;
        }

        public override async Task<ResponseEntity<PagedResultDto<PromoteSalesFullCut>>> QueryPaging([FromBody] PagingRequestDto dto)
        {
            var res = await base.QueryPaging(dto);

            if (res.Data?.Items?.Any() ?? false)
            {
                res.Data.Items = await this.PromotionSalesFullCutService.AttachWarehouse(res.Data.Items.ToArray());
                res.Data.Items = await this.PromotionSalesFullCutService.AttachGoodsCategory(res.Data.Items.ToArray());
            }

            return res;
        }

        [HttpPost("approve")]
        public async Task<ResponseEntity<object>> ApprovePromotionSalesFullCut(PromotionSalesFullCutApprovementDto dto)
        {
            await this.PromotionSalesFullCutService.ApprovePromotionSalesFullCut(dto);

            return new ResponseEntity<object>().SetSuccess();
        }

        [HttpPost("reject")]
        public async Task<ResponseEntity<object>> RejectPromotionSalesFullCut(PromotionSalesFullCutApprovementDto dto)
        {
            await this.PromotionSalesFullCutService.RejectPromotionSalesFullCut(dto);

            return new ResponseEntity<object>().SetSuccess();
        }

        [HttpPost("save-multiple")]
        public async Task<ResponseEntity<PromoteSalesFullCut[]>> SaveMultiple([FromBody] ReceiptNoItemsDto<PromoteSalesFullCut> data)
        {
            var res = await this.Repo.SaveReceiptItems(this.HttpContext.RequestServices, data, "FC");
            return new ResponseEntity<PromoteSalesFullCut[]>().SetData(res.Items);
        }
    }
}
